package com.stylefeng.guns.rest.modular.sms.controller;

import com.aliyuncs.exceptions.ClientException;
import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.enums.MsgCode;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.common.persistence.model.SmsCode;
import com.stylefeng.guns.rest.common.utils.InputCheckUtil;
import com.stylefeng.guns.rest.common.utils.TextUtil;
import com.stylefeng.guns.rest.modular.sms.controller.dto.SmsCodeGetRequest;
import com.stylefeng.guns.rest.modular.sms.service.SmsCodeService;
import com.stylefeng.guns.rest.modular.users.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by ASUS-PC on 2018/6/21.
 * 短信验证码控制器
 */
@RestController
@RequestMapping("/smsCode")
public class SmsCodeController {
    @Autowired
    SmsCodeService smsCodeService;
    @Autowired
    UserService userService;

    //获取验证码
    @RequestMapping(value = "/getCode" , method = RequestMethod.POST)
    public ResponseEntity<?> getCode(@RequestBody SmsCodeGetRequest request) throws ClientException {
        if (TextUtil.isEmpty(request.getMobile())
                || request.getType() == 0){
            return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.NEED_PARAMS,"缺少参数" , ""));
        }
        if(TextUtil.isEmpty(request.getCode())){
            request.setCode("86");
        }
        if("86".equals(request.getCode())){
            if(!InputCheckUtil.isVaildPhone(request.getMobile())){
                return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.INVALID_PHONE_NUMBER,"手机号不合法" , ""));
            }
        }
        if(request.getType()==1){//注册验证码
            int s = userService.checkUserIsRegiset(request.getMobile());
            if(s==1){
                throw new BussinessException(BizExceptionEnum.USER_IS_REGISTER);
            }

            int state = smsCodeService.inserCode(request);
            if(state==1){
                return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.SUCCESS_CODE,"" , ""));
            }else {
                return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.ERROR_CODE,"获取验证码失败" , ""));
            }
        }else if(request.getType() == 2){//重设密码获取验证码
            if (!userService.hasAccount(request.getMobile())){
                return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.ERROR_CODE,"账号不存在" , ""));
            }else {
                int smsCode = 0;
                try {
                    smsCode = smsCodeService.inserCode(request);
                } catch (ClientException e) {
                    e.printStackTrace();
                }
                if(smsCode==1){
                    return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.SUCCESS_CODE,"获取验证码成功" , ""));
                }else {
                    return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.ERROR_CODE,"获取验证码失败" , ""));
                }
            }
        }else if(request.getType() == 3){//验证码登陆获取验证码
            int smsCode = 0;
            try {
                smsCode = smsCodeService.inserCode(request);
            } catch (ClientException e) {
                e.printStackTrace();
            }
            if(smsCode==1){
                return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.SUCCESS_CODE,"获取验证码成功" , ""));
            }else {
                return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.ERROR_CODE,"获取验证码失败" , ""));
            }
        }
        return ResponseEntity.ok(CommonResponse.okMsg(MsgCode.ERROR_CODE,"获取验证码失败" , ""));
    }

    //获取国家编码列表
    @RequestMapping(value = "/getCountryCode" , method = RequestMethod.POST)
    public ResponseEntity<?> getCountryCodeList(){
        return ResponseEntity.ok(CommonResponse.ok(smsCodeService.getCountryCode()));
    }

}
